package com.finesys.backend.user.service.redis;

import com.finesys.backend.common.model.user.UserModel;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author lehoon
 * <p>Description: RedisService</p>
 * 2020年10月12日
 */
@Service("redisService")
public class RedisService {
	@Resource
	private StringRedisTemplate stringRedisTemplate;
	
	@Resource
	private RedisTemplate<String, Object> redisTemplate;

	public void evictRedis(String key) {
		redisTemplate.delete(key);
	}
	
	public void evictStringRedis(String key) {
		stringRedisTemplate.delete(key);
	}
	
	public void setSmsCode(String key, String code) {
		stringRedisTemplate.opsForValue().set(key, code, 60*10, TimeUnit.SECONDS);
	}
	
	public String getSmsCode(String key) {
		return stringRedisTemplate.boundValueOps(key).get();
	}
	
	public void setValueCache(String key, Object o) {
		redisTemplate.opsForValue().set(key, o);
	}
	
	public Object getValueCache(String key) {
		return redisTemplate.boundValueOps(key).get();
	}

	public void setListVal(String key, List val) {
		redisTemplate.opsForList().leftPushAll(key, val);
	}
	
	public List<Object> getListVal(String key) {
		return redisTemplate.opsForList().range(key, 0, -1);
	}
	
	public void removeListVal(String key) {
		redisTemplate.opsForList().remove(key, 0, -1);
	}
	
	public void setSetVal(String key, Object val) {
		redisTemplate.opsForSet().add(key, val);
	}
	
	public int getSetLenth(String key) {
		return redisTemplate.opsForSet().members(key).size();
	}
	
	public Boolean isMembers(String key, Object val) {
		return redisTemplate.opsForSet().isMember(key, val);
	}
	
	public Set<Object> sDiff(String keyA, String keyB) {
		return redisTemplate.opsForSet().difference(keyA, keyB);
	}
	
	public Set<Object> sInter(String keyA, String keyB) {
		return redisTemplate.opsForSet().intersect(keyA, keyB);
	}
	
	public void sRem(String key, Object val) {
		redisTemplate.opsForSet().remove(key, val);
	}
	
	public Set<Object> sMembers(String key) {
		return redisTemplate.opsForSet().members(key);
	}
	
	public void incr(String key) {
		stringRedisTemplate.boundValueOps(key).increment(1);
	}
	

	public void decr(String key) {
		stringRedisTemplate.boundValueOps(key).increment(-1);
	}
	
	public String get(String key) {
		return stringRedisTemplate.boundValueOps(key).get();
	}
	
	public void setZsetVal(String key, Object val, int score) {
		redisTemplate.opsForZSet().add(key, val, score);
	}
	
	public void sRemZsetVal(String key, Object val) {
		redisTemplate.opsForZSet().remove(key, val);
	}
	
	//判断是否存在，没有相关api，主要用于会员用户医生用户之间的关注判断
	public Boolean bZSetScore(String key, Object val) {
		return null != redisTemplate.opsForZSet().score(key, val)?true:false;
	}
	
	public Set<Object> zRange(String key, int start, int end) {
		return redisTemplate.opsForZSet().range(key, start, end);
	}
	
	public void zincrby(String key, Object val, double num) {
		redisTemplate.boundZSetOps(key).incrementScore(val, num);
	}
	
	public Set<Object> zrange(String key, int start, int end) {
		return redisTemplate.boundZSetOps(key).range(start, end);
	}
	
	public Long zcard(String key) {
		return redisTemplate.boundZSetOps(key).zCard();
	}

	public void setUserForWeb(String token, UserModel userModel) {
		redisTemplate.opsForValue().set(token, userModel, 60*60*4, TimeUnit.SECONDS);
	}
	public void setUserIdForWeb(String refeshToken, String userId) {
		redisTemplate.opsForValue().set(refeshToken, userId, 60*60*8, TimeUnit.SECONDS);
	}

	public void setUserForApp(String token, UserModel userModel) {
		redisTemplate.opsForValue().set(token, userModel, 60*60*12, TimeUnit.SECONDS);
	}

	public void setUserIdForApp(String refeshToken, String userId) {
		redisTemplate.opsForValue().set(refeshToken, userId, 60*60*24, TimeUnit.SECONDS);
	}

	public UserModel getUserByToken(String token) {
		return (UserModel) redisTemplate.boundValueOps(token).get();
	}

	public String getUserIdByToken(String token) {
		return (String) redisTemplate.boundValueOps(token).get();
	}

	public void deleteUserToken(String token) {
		redisTemplate.boundValueOps(token).expire(0, TimeUnit.SECONDS);
	}

	public Long getExpire(String token){
		return redisTemplate.opsForValue().getOperations().getExpire(token, TimeUnit.SECONDS);
	}
}
